﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Dao;

namespace Web.Page
{
    public partial class KC_WLPD_Query : System.Web.UI.Page
    {
        protected readonly string RESULT = "result";

        protected KC_WLPD_Dao dao = new KC_WLPD_Dao();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                initValue();
            }
        }

        private void initValue()
        {
            // 仓位
            ZL_CW_Dao daoCW = new ZL_CW_Dao();
            List<ZL_CW> lsCW = daoCW.QueryAll();
            this.ddlCWID.DataSource = lsCW;
            this.ddlCWID.DataValueField = "ID";
            this.ddlCWID.DataTextField = "BH";
            this.ddlCWID.DataBind();
            this.ddlCWID.Items.Insert(0, new ListItem("请选择", "0"));

            // 仓库
            ZL_CK_Dao daoCK = new ZL_CK_Dao();
            List<ZL_CK> lsCK = daoCK.QueryAll();
            this.ddlCKID.DataSource = lsCK;
            this.ddlCKID.DataValueField = "ID";
            this.ddlCKID.DataTextField = "MC";
            this.ddlCKID.DataBind();
            this.ddlCKID.Items.Insert(0, new ListItem("请选择", "0"));
        }

        protected void btnQuery_Click(object sender, ImageClickEventArgs e)
        {
            this.Validate();
            if (IsValid)
            {
                BindGridview();
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                vWLKC item = (vWLKC)e.Row.DataItem;
                ((CheckBox)(e.Row.Cells[0].FindControl("cbSelect"))).Text = item.BH;
            }
        }

        protected List<vWLKC> query()
        {
            string bh = this.txtBH.Text;
            string mc = this.txtMC.Text;
            string ckid = this.ddlCKID.SelectedValue;
            string cwid = this.ddlCWID.SelectedValue;

            return dao.QueryBySql(bh, mc, ckid, cwid);
        }

        private void BindGridview()
        {
            ZL_WL_Dao daoWL = new ZL_WL_Dao();
            List<ZL_WL> lsWL = daoWL.QueryAll();

            // 仓位
            ZL_CW_Dao daoCW = new ZL_CW_Dao();
            List<ZL_CW> lsCW = daoCW.QueryAll();

            // 仓库
            ZL_CK_Dao daoCK = new ZL_CK_Dao();
            List<ZL_CK> lsCK = daoCK.QueryAll();

            // GridView1.PageIndex = RestorePageIndex();

            List<vWLKC> list = query();

            if (list != null)
            {
                this.GridView1.DataSource = list;
                this.GridView1.DataBind();
                Cache.Insert(RESULT, list);
            }
        }

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            string bh = this.txtBH.Text;
            string mc = this.txtMC.Text;
            string ckid = this.ddlCKID.SelectedValue;
            string cwid = this.ddlCWID.SelectedValue;

            if (String.IsNullOrWhiteSpace(bh) && String.IsNullOrWhiteSpace(mc) && "0".Equals(ckid) && "0".Equals(cwid))
            {
                this.CustomValidator1.IsValid = false;
                args.IsValid = false;
            }
            else
            {
                this.CustomValidator1.IsValid = true;
                args.IsValid = true;
            }
        }

        protected void btnExport_Click(object sender, ImageClickEventArgs e)
        {
            this.lbResult.Text = "";

            string bh = this.txtBH.Text;
            string mc = this.txtMC.Text;
            string ckid = this.ddlCKID.SelectedValue;
            string cwid = this.ddlCWID.SelectedValue;

            this.Validate();
            if (IsValid)
            {
                string url = String.Format("Export.aspx?type=csv&bh={0}&mc={1}&ckid={2}&cwid={3}", HttpUtility.UrlEncode(bh), HttpUtility.UrlEncode(mc), ckid, cwid);
                // ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "onExport", String.Format("<script type='text/javascript'>open('{0}');</script>", url), false);
                Response.Redirect(url);
                this.UpdatePanel1.Visible = false;
                //string temp = string.Format("attachment;filename={0}", "erp.csv");
                //// Response.Clear();
                //Response.ContentType = "application/ms-excel"; // "text/csv";//application/ms-excel
                //Response.AppendHeader("Content-Disposition", temp);
                //Response.ContentEncoding = Encoding.Default;
                //Response.Write(exportCsv());
                //Response.Flush();
                //Response.End();
            }
        }

        protected void btnRevise_Click(object sender, ImageClickEventArgs e)
        {
            this.lbResult.Text = "";

            // StringBuilder sb = new StringBuilder();
            List<vWLKC> list = (List<vWLKC>)Cache.Get(RESULT);
            List<vWLKC> list2 = new List<vWLKC>();
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                GridViewRow row = GridView1.Rows[i];
                vWLKC item = list[i];
                bool selected = ((CheckBox)(row.Cells[0].FindControl("cbSelect"))).Checked;
                if (selected)
                {
                    // sb.Append(item.ID);
                    // sb.Append(";");

                    list2.Add(item);
                }
            }

            // this.lbResult.Text = sb.ToString();

            if (list2.Count == 0)
            {
                this.lbResult.Text = "请选择要修订的记录。";
            }
            else
            {
                Cache.Insert(KC_WLPD_Edit.SELECTED, list2);
                Response.Redirect("KC_WLPD_Edit.aspx");
            }
        }

        protected string exportCsv()
        {
            List<vWLKC> list = query();
            if (list != null)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("编号");
                sb.Append(",名称");
                sb.Append(",类型");
                sb.Append(",数量");
                sb.Append(",安全数量");
                sb.Append(",单位");
                sb.Append(",仓库");
                sb.Append(",仓位");
                sb.Append(",供应商");
                sb.Append("\r\n");

                foreach (vWLKC item in list)
                {
                    sb.Append(item.BH);
                    sb.Append(",");
                    sb.Append(item.MC);
                    sb.Append(",");
                    sb.Append(item.LXMC);
                    sb.Append(",");
                    sb.Append(item.SL);
                    sb.Append(",");
                    sb.Append(item.AQKCSL);
                    sb.Append(",");
                    sb.Append(item.DWMC);
                    sb.Append(",");
                    sb.Append(item.CKMC);
                    sb.Append(",");
                    sb.Append(item.CWBH);
                    sb.Append(",");
                    sb.Append(item.GYSMC);
                    sb.Append("\r\n");
                }

                return sb.ToString();
            }

            return null;
        }
    }
}